package oop.lab.console.gameview;

import java.util.List;

import oop.lab.console.BoardViewer;
import oop.lab.console.command.Command;
import oop.lab.console.command.NewGameCommand;
import oop.lab.console.command.PlacePieceCommand;
import oop.lab.console.command.QuitCommand;
import oop.lab.console.command.SelectGameCommand;
import oop.lab.console.playground.PlayGround;
import oop.lab.console.screen.Screen;
import oop.lab.domain.board.Location;
import oop.lab.domain.game.GomokuGame;

public class GomokuGameView extends GameViewer {
    private final GomokuGame game;
    private final BoardViewer boardUI;

    public static GomokuGameView create() {
        return new GomokuGameView(new GomokuGame());
    }

    public GomokuGameView(GomokuGame game, String gameTag) {
        super(gameTag);
        this.game = game;
        this.boardUI = new BoardViewer(game.getBoard());
    }

    public GomokuGameView(GomokuGame game) {
        this(game, "");
    }

    @Override
    public GomokuGame getGame() {
        return game;
    }

    @Override
    public void showBoard(Screen screen) {
        boardUI.display(screen);
    }

    @Override
    public String getGameStatus() {
        if (game.isOver()) {
            if (game.getWinner() == null)
                return "游戏结束：平局";
            else
                return String.format("游戏结束：%s胜", game.getWinner());
        } else {
            return "";
        }
    }

    @Override
    public List<Command> getCommandList(PlayGround session) {
        return List.of(
                new PlacePieceCommand(game, session) {
                            @Override
                    public boolean isEnabled() {
                        return !game.isOver();
                    }
                },
                new SelectGameCommand(session),
                new NewGameCommand(session),
                new QuitCommand());
    }

    @Override
    public Location getSize() {
        return Location.of(game.getBoard().getSize(), game.getBoard().getSize());
    }

}